import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/hj_page/main_page/provider/msg_provider.dart';
import 'package:tiktokadult/hj_utils/date_time_util.dart';
import 'package:tiktokadult/hj_utils/holiday_theme.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';

import '../../../tools_base/global_store/store.dart';
import '../mine_vip/mine_charge_vip_page.dart';

//信任倒计时
class VipTimeAcView extends StatefulWidget {
  const VipTimeAcView({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _VipTimeAcView();
  }
}

class _VipTimeAcView extends State<VipTimeAcView> {
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () => Get.to(() => MineChargeVipPage()),
      child: Padding(
        padding: EdgeInsets.symmetric(horizontal: 16),
        child: Stack(
          alignment: Alignment.centerLeft,
          children: [
            Image.asset(
              HolidayTheme.theme.vipBg,
              width: double.infinity,
              fit: BoxFit.fill,
              height: 54,
            ),
            Consumer<GlobalStore>(
              builder: (context, global, child) => Consumer<MineMsgProvider>(
                builder: (context, provider, child) {
                  if (global.isVIP()) {
                    return _buildVip(global);
                  } else {
                    if ((provider.countdown?.leftTimeSinceEpoch ?? 0) > 0) {
                      return _buildNewer(provider);
                    } else {
                      return _buildNormal();
                    }
                  }
                },
              ),
            )
          ],
        ),
      ),
    );
  }

  _buildNewer(MineMsgProvider provider) {
    String text =
        '会员中心 新用户开通会员特惠';
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10,vertical: 3),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            text,
            style: TextStyle(color: Colors.white, fontSize: 14, fontWeight: FontWeight.w600),
          ),
          5.sizeBoxH,
          Row(
            children: [
              Container(
                alignment: Alignment.center,
                width: 70,
                padding: EdgeInsets.symmetric(horizontal: 3,vertical: 2),
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10),
                    gradient: LinearGradient(
                      colors: [Color(0xffE3B284), Color(0xffFEEEDE)],
                      tileMode: TileMode.mirror,
                    )
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text(
                      '马上开通',
                      style: TextStyle(fontSize: 10,color: Color(0xFF292929)),
                    ),
                    Icon(Icons.arrow_forward_ios,color: Color(0xFF292929),size: 10),
                  ],
                ),
              ),
              10.sizeBoxW,
              Row(
                children: [
                  Container(
                    padding: EdgeInsets.symmetric(horizontal: 2,vertical: 0),
                    decoration: BoxDecoration(
                      color: Color(0xFFFE3653),
                      borderRadius: BorderRadius.circular(5)
                    ),
                    child: Text("${provider.countdown?.hourDesc ?? '00'}",style: TextStyle(fontSize: 12,color: Colors.white)),
                  ),
                  Text(" : ",style: TextStyle(fontSize: 12,color: Colors.white)),
                  Container(
                    padding: EdgeInsets.symmetric(horizontal: 2,vertical: 0),
                    decoration: BoxDecoration(
                        color: Color(0xFFFE3653),
                        borderRadius: BorderRadius.circular(5)
                    ),
                    child: Text("${provider.countdown?.minDesc ?? '00'}",style: TextStyle(fontSize: 12,color: Colors.white)),
                  ),
                  Text(" : ",style: TextStyle(fontSize: 12,color: Colors.white)),
                  Container(
                    padding: EdgeInsets.symmetric(horizontal: 2,vertical: 0),
                    decoration: BoxDecoration(
                        color: Color(0xFFFE3653),
                        borderRadius: BorderRadius.circular(5)
                    ),
                    child: Text("${provider.countdown?.secondsDesc ?? '00'}",style: TextStyle(fontSize: 12,color: Colors.white)),
                  ),
                ],
              )
            ],
          ),
          16.sizeBoxW,
        ],
      ),
    );
  }

  _buildNormal() {
    String text = '会员中心 限时特惠 畅看全场';
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10,vertical: 3),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            text,
            style: TextStyle(color: Colors.white, fontSize: 14, fontWeight: FontWeight.w600),
          ),
          5.sizeBoxH,
          Container(
            alignment: Alignment.center,
            width: 70,
            padding: EdgeInsets.symmetric(horizontal: 3,vertical: 2),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
                gradient: LinearGradient(
                  colors: [Color(0xffE3B284), Color(0xffFEEEDE)],
                  tileMode: TileMode.mirror,
                )
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Text(
                  '马上开通',
                  style: TextStyle(fontSize: 10,color: Color(0xFF292929)),
                ),
                Icon(Icons.arrow_forward_ios,color: Color(0xFF292929),size: 10),
              ],
            ),
          ),
          16.sizeBoxW,
        ],
      ),
    );
  }

  _buildVip(GlobalStore global) {
    int level = global.meInfo?.vipLevel ?? 0;
    String vipTitle = '';
    if (level == 1) vipTitle = '普通会员';
    if (level == 2) vipTitle = 'SVIP';
    if (level >= 3) vipTitle = '至尊永久卡';
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10,vertical: 3),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          ShaderMask(
            shaderCallback: (Rect bounds) {
              return LinearGradient(
                colors: [Color(0xffE3B284), Color(0xffFEEEDE)],
                tileMode: TileMode.mirror,
              ).createShader(bounds);
            },
            blendMode: BlendMode.srcATop,
            child: Center(
              child: Row(
                children: [
                  Text(
                    "${globalStore.meInfo?.vipName == null || globalStore.meInfo?.vipName == '' ? '普通会员' : globalStore.meInfo?.vipName}",
                    style: TextStyle(color: Colors.white, fontSize: 14, fontWeight: FontWeight.w600),
                  ),
                  Text(
                    ': ${DateTimeUtil.utcTurnYear(global.meInfo?.vipExpireDate, char: ".")}',
                    style: TextStyle(fontSize: 14),
                  ),
                ],
              )
            ),
          ),
          5.sizeBoxH,
          Container(
            alignment: Alignment.center,
            width: 70,
            padding: EdgeInsets.symmetric(horizontal: 3,vertical: 2),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
                gradient: LinearGradient(
                  colors: [Color(0xffE3B284), Color(0xffFEEEDE)],
                  tileMode: TileMode.mirror,
                )
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Text(
                  '升级会员',
                  style: TextStyle(fontSize: 10,color: Color(0xFF292929)),
                ),
                Icon(Icons.arrow_forward_ios,color: Color(0xFF292929),size: 10),
              ],
            ),
          ),
          16.sizeBoxW,
        ],
      ),
    );
  }
}
